﻿//作者：       minjie.zhou
// 创建时间：   2012/12/11 23:42:52
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using AgileToDo.Common;
using AgileToDo.ViewModel;
using YDNoteOpenAPI4N;
using YDNoteOpenAPI4N.DataModel;
using YDNoteOpenAPI4N.YDAPI;
using YDNoteOpenAPI4N.YDConsumer;
using mshtml;

namespace AgileToDo
{
    /// <summary>
    /// PageLogin.xaml 的交互逻辑
    /// </summary>
    public partial class PageLogin : Page
    {
        public PageLogin()
        {
            InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
          
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            progressBar.Start();
            var threadStart = new ThreadStart(()=>
                                                 {
                                                     var loginVm = Application.Current.FindResource("pageLoginVM") as PageLoginViewModel;
                                                     var uri =
                                                               YDDesktopConsumer.RequestAuthorization(
                                                                   OAuthHelper.Consumer, out OAuthHelper.RequestToken);
                                                     var win = new WinLoginHide();
                                                     win.UserName = loginVm.UserName;
                                                     win.Password = loginVm.Password;
                                                     win.wb.Source = uri;
                                                     win.LoginSuccess += () =>
                                                                             {
                                                                                 win.Close();
                                                                                 this.Dispatcher.BeginInvoke(
                                                                                new Action(() =>
                                                                                {
                                                                                    var nvs = NavigationService.GetNavigationService(this);
                                                                                    nvs.Navigate(new PageMain());
                                                                                }));
                                                                             };
                                                     win.Closed += (a, b) =>
                                                     Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background);
                                                     win.Show();
                                                     Dispatcher.Run();
                                                 });
            var thread = new Thread(threadStart);
            thread.SetApartmentState(ApartmentState.STA);

            thread.Start();


        }


    }
}
